
import {BrowserWindow, ipcMain,dialog} from 'electron'
import {readFile,readFileDic,openFile} from "../file/demo";
import windowControl from "../windowControl";

export default function (){
  ipcMain.handle('getFile',async (event,path)=>{
    let resData={
      code:0,
      data:{},
      message:"",
    }
    try{
      let res:any = await readFile(path)
      resData.data = res.toString();
    }catch (e:any) {
      resData.code = 400;
      resData.message = e;
    }
    return resData
  });
  ipcMain.handle('toTop',async (event,topStatus:boolean)=>{
     try{
       windowControl.windowExtraConfig.isTop = topStatus;
       windowControl.windows.forEach((item:{id:string,window:BrowserWindow})=>{
         item.window.setAlwaysOnTop(topStatus);
       })
       return 'success'
     }catch (e) {
       console.log(e);
       return "error"
     }
  });
  ipcMain.handle('openNewWindow',async (event)=>{
     try{
       windowControl.createWindow();
       return 'success'
     }catch (e) {

       return "error"
     }
  });
  ipcMain.handle('openFileDirect',async (event)=>{
     try{

      let result = await readFileDic();
      console.log(result)
       return 'success'
     }catch (e) {

       return "error"
     }
  });
  ipcMain.handle('openFile',async (event)=>{
     try{

      let result = await openFile();
      console.log(result)
       return result
     }catch (e) {

       return null
     }
  });
}
